﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmThemHoaDonBanVe : Form
    {
        public frmThemHoaDonBanVe()
        {
            InitializeComponent();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }
        public List<LoaiVeDTO> dsloaive;
        private void frmThemHoaDonBanVe_Load(object sender, EventArgs e)
        {
            try
            {
                // load loai ve
                LoaiVeBUS lvbus = new LoaiVeBUS();
                dsloaive = new List<LoaiVeDTO>();
                dsloaive = lvbus.laydanhsachloaive();
                cmb_loaive.DataSource = dsloaive;
                cmb_loaive.ValueMember = "MaLoai";
                cmb_loaive.DisplayMember = "Ten";
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        private void dgvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvChiTiet.CurrentCell.ColumnIndex == 2)
            {
                ComboBox cbx = e.Control as ComboBox;


                if (cbx != null)
                {
                    //cmb_dgv_TenNVL.DataSource = dsnvl;
                    //cmb_dgv_TenNVL.DisplayMember = "Ten";
                    //cmb_dgv_TenNVL.ValueMember = "MaNVL";

                    cbx.SelectedIndexChanged -= comboBox_SelectedIndexChanged;
                    cbx.SelectedIndexChanged += comboBox_SelectedIndexChanged;
                }




            }
        }

        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int selectedIndex = ((ComboBox)sender).SelectedIndex;
            //MessageBox.Show("Selected Index = " + selectedIndex);
            try
            {
                int mave = Convert.ToInt32(((ComboBox)sender).SelectedValue);
                foreach (LoaiVeDTO lv in dsloaive)
                {
                    if (lv.MaLoai == mave)
                    {
                        Double giaban = lv.Gia;
                        string ten = lv.Ten;
                        
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[1].Value = mave.ToString();
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[3].Value = ten;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[5].Value = giaban;

                    }
                }
            }
            catch (Exception)
            {
            }

        }

        private void dgvChiTiet_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                try
                {
                    if (dgvChiTiet.Rows[e.RowIndex].Cells[4].Value.ToString() != "" && dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString() != "")
                    {
                        int soluong = Convert.ToInt32(dgvChiTiet.Rows[e.RowIndex].Cells[4].Value.ToString());
                        double dongia = Convert.ToDouble(dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString());
                        double thanhtien = soluong * dongia;
                        dgvChiTiet.Rows[e.RowIndex].Cells[6].Value = thanhtien.ToString();
                    }

                }

                catch (Exception)
                {

                }
            }

            // add stt
            int stt = 1;
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                dgvChiTiet.Rows[i].Cells[0].Value = stt.ToString();
                stt++;
            }

            // tinh tong tien

            double tongtien = 0;
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                try
                {
                    if (dgvChiTiet.Rows[i].Cells[6].Value != "")
                    {
                        double thanhtien = Convert.ToDouble(dgvChiTiet.Rows[i].Cells[6].Value);
                        tongtien += thanhtien;
                    }
                }
                catch (Exception)
                {


                }
            }

            lbTongTien.Text = tongtien.ToString();

        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            // them hoa don
            HoaDonBanVeBUS hdbvbus = new HoaDonBanVeBUS();
            HoaDonBanVeDTO hdbvdto = new HoaDonBanVeDTO();
            hdbvdto.NgayLap = Convert.ToDateTime(dtpNgayLap.Text);
            hdbvdto.TongTien = Convert.ToDouble(lbTongTien.Text.ToString());
            hdbvdto.MaKH = Convert.ToInt32(txtmakh.Text.ToString());


            int ID = hdbvbus.ThemHoaDonBanVe(hdbvdto);

            if (ID == -1)
            {
                MessageBox.Show("them hoa don that bai");
                return;
            }

            // them chi tiet
            CT_HoaDonBanVeBUS cthdbus = new CT_HoaDonBanVeBUS();
            string mes = "";
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                try
                {
                    CT_HoaDonBanVeDTO cthdbv= new CT_HoaDonBanVeDTO();
                    cthdbv.MaHD = ID;
                    cthdbv.MaLoai = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[1].Value);
                    cthdbv.SoLuong = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[4].Value);
                   
                    if (cthdbv.MaLoai != 0)
                    {
                       mes =  cthdbus.ThemChiTietHoaDonbanVe(cthdbv);
                    }


                }
                catch (Exception)
                {


                }


            }
            if (mes != "")
            {
                MessageBox.Show(mes);
            }
            else
            {
                MessageBox.Show("them thanh cong");
            }
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            int id = dgvChiTiet.CurrentCell.RowIndex;
            dgvChiTiet.Rows.RemoveAt(id);
        }

    }
}
